package com.andy.blog.dal.service.impl;

import com.andy.blog.common.constant.ScheduleConstants;
import com.andy.blog.common.exception.BlogServiceException;
import com.andy.blog.common.reponse.BlogResultCode;
import com.andy.blog.common.utils.DateUtils;
import com.andy.blog.common.utils.SecurityUtils;
import com.andy.blog.common.utils.TransformUtils;
import com.andy.blog.dal.domain.SysJobDO;
import com.andy.blog.dal.mapper.SysJobMapper;
import com.andy.blog.dal.service.SysJobDalService;
import com.andy.blog.dal.vo.SysJobVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * Created by andy on 2023/5/9 18:24
 */
@Service
public class SysJobDalServiceImpl implements SysJobDalService {

    @Resource
    private SysJobMapper sysJobMapper;

    /**
     * 构建查询条件
     */
    private LambdaQueryWrapper<SysJobDO> buildQueryWrapper(SysJobVO sysJobVO) {
        LambdaQueryWrapper<SysJobDO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (Objects.isNull(sysJobVO)) {
            return lambdaQueryWrapper;
        }

        lambdaQueryWrapper
                .like(StringUtils.isNotBlank(sysJobVO.getJobName()), SysJobDO::getJobName, sysJobVO.getJobName())
                .eq(StringUtils.isNotBlank(sysJobVO.getJobGroup()), SysJobDO::getJobGroup, sysJobVO.getJobGroup())
                .eq(StringUtils.isNotBlank(sysJobVO.getStatus()), SysJobDO::getStatus, sysJobVO.getStatus())
                .like(StringUtils.isNotBlank(sysJobVO.getInvokeTarget()), SysJobDO::getInvokeTarget, sysJobVO.getInvokeTarget());

        return lambdaQueryWrapper;
    }

    @Override
    public IPage<SysJobDO> selectByList(SysJobVO sysJobVO) {
        LambdaQueryWrapper<SysJobDO> lambdaQueryWrapper = buildQueryWrapper(sysJobVO);

        IPage<SysJobDO> page = new Page<>(sysJobVO.getPageNum(), sysJobVO.getPageSize());

        return sysJobMapper.selectPage(page, lambdaQueryWrapper);
    }

    @Override
    public List<SysJobDO> selectListByCondition(SysJobVO sysJobVO) {
        LambdaQueryWrapper<SysJobDO> lambdaQueryWrapper = buildQueryWrapper(sysJobVO);

        return sysJobMapper.selectList(lambdaQueryWrapper);
    }

    @Override
    public SysJobDO selectById(Long jobId) {
        return sysJobMapper.selectById(jobId);
    }

    @Override
    public void insertSelective(SysJobVO sysJobVO) {
        SysJobDO sysJobDO = TransformUtils.transform(sysJobVO, SysJobDO.class);

        sysJobDO.setStatus(ScheduleConstants.Status.PAUSE.getValue());
        sysJobDO.setCreateTime(DateUtils.getCurrentDate());
        sysJobDO.setCreateBy(SecurityUtils.getUsername());

        int row;
        try {
            row = sysJobMapper.insert(sysJobDO);
        } catch (Exception e) {
            throw new BlogServiceException(BlogResultCode.BLOG_000100, e);
        }

        if (!Objects.equals(row, 1)) {
            throw new BlogServiceException(BlogResultCode.BLOG_000100);
        }
    }

    @Override
    public int deleteJobById(Long jobId) {
        return sysJobMapper.deleteById(jobId);
    }

    @Override
    public void updateJob(SysJobVO sysJobVO) {
        SysJobDO sysJobDO = TransformUtils.transform(sysJobVO, SysJobDO.class);

        sysJobDO.setUpdateBy(SecurityUtils.getUsername());
        sysJobDO.setUpdateTime(DateUtils.getCurrentDate());

        int row;
        try {
            row = sysJobMapper.updateById(sysJobDO);
        } catch (Exception e) {
            throw new BlogServiceException(BlogResultCode.BLOG_000101, e);
        }

        if (!Objects.equals(row, 1)) {
            throw new BlogServiceException(BlogResultCode.BLOG_000100);
        }
    }
}
